package com.example.barcodescanner;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class RegisterActivity extends Activity{
	Button Register;
	EditText UserId;
	EditText Email;
	EditText firstName;
	EditText lastName;
	EditText Phone;
	EditText Password;
	EditText Account;
	EditText address;
	EditText City;
	EditText State;
	EditText PostalCode;
	EditText CountryCode;
	
	
	
	@Override
	public void onCreate(Bundle instanceState) {
		super.onCreate(instanceState);
		setContentView(R.layout.activity_register);

		Register = (Button) findViewById(R.id.Register);
		UserId = (EditText) findViewById(R.id.userId);
		Email = (EditText) findViewById(R.id.Email);
		firstName  = (EditText) findViewById(R.id.firstName);
		lastName  = (EditText) findViewById(R.id.lastName);
		Phone  = (EditText) findViewById(R.id.Phone);
		Password  = (EditText) findViewById(R.id.Password);
		Account  = (EditText) findViewById(R.id.Account);
		address  = (EditText) findViewById(R.id.address);
		City  = (EditText) findViewById(R.id.City);
		State  = (EditText) findViewById(R.id.State);
		PostalCode  = (EditText) findViewById(R.id.PostalCode);
		CountryCode  = (EditText) findViewById(R.id.CountryCode);
		
		
		

		Register.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String userId = UserId.getText().toString();
				String email = Email.getText().toString();
				String fName = firstName.getText().toString();
				String lName = lastName.getText().toString();
				String phone = Phone.getText().toString();
				String password = Password.getText().toString();
				String account = Account.getText().toString();
				String add = address.getText().toString();
				String city = City.getText().toString();
				String state = State.getText().toString();
				String postalCode = PostalCode.getText().toString();
				String countryCode=CountryCode.getText().toString();
				
				MessageDigest m = null;
	        	try {
	        	m = MessageDigest.getInstance("MD5");
	        	} catch (NoSuchAlgorithmException e1) {
	        	// TODO Auto-generated catch block
	        	e1.printStackTrace();
	        	}
	        	m.reset();
	        	m.update(password.getBytes());
	        	byte[] digest = m.digest();
	        	BigInteger bigInt = new BigInteger(1, digest);
	        	String hashtext = bigInt.toString(16);
	        	// Now we need to zero pad it if you actually want the full 32
	        	// chars.
	        	while (hashtext.length() < 32) {
	        	hashtext = "0" + hashtext;
	        	}   	
				
						
				String json = "{  \"udfId\": \""+userId+"\",\"email\": \""+email+"\",\"firstName\": \""+fName+"\",\"lastName\":\""+lName+"\",\"phone\": \""+phone+"\",\"password\":\""+hashtext+"\",\"account\": {   \"organization\": \""+account+"\"  },\"address\": {    \"city\": \""+city+"\",\"countryCode\": \""+countryCode+"\",\"postalCode\": \""+postalCode+"\",\"stateCode\": \""+state+"\",\"streetAddress1\": \""+add+"\"  }}";					
			
				
				
				HttpClient httpClient = new DefaultHttpClient();

			    try {
			        HttpPost request = new HttpPost("http://adtouch.cloudfoundry.com/rest/user");
			        StringEntity params =new StringEntity(json);
			        request.addHeader("Accept","application/json");
			        request.addHeader("Content-Type", "application/json");
			        request.setEntity(params);
			        HttpResponse httpResponse = httpClient.execute(request);
			        
			        System.out.println("json is : " + json);
			        System.out.println("response is: " + httpResponse.getStatusLine().getStatusCode());
			        
			        if(httpResponse.getStatusLine().getStatusCode() == 201)
			        {
			        	Toast toast = Toast.makeText(getApplicationContext(), "Object Created!!",
				   				 Toast.LENGTH_LONG);
				   				 toast.show();
			        }
			        else if(httpResponse.getStatusLine().getStatusCode() == 409)
			        {
			        	Toast toast = Toast.makeText(getApplicationContext(), "There is a Conflict in Data!!",
				   				 Toast.LENGTH_LONG);
				   				 toast.show();
			        }
			        else if(httpResponse.getStatusLine().getStatusCode() == 400)
			        {
			        	Toast toast = Toast.makeText(getApplicationContext(), "Bad Request!, Data Invalid!!",
				   				 Toast.LENGTH_LONG);
				   				 toast.show();
			        }

			        // handle response here...
			    }catch (Exception ex) {
			        // handle exception here
			    } finally {
			        httpClient.getConnectionManager().shutdown();
			    }
				
			
		            
				
				
			}
		});
	}

	
	
	

}
